Imports Telerik.WebControls
Imports System.Data
Imports System.Data.SqlClient

Partial Class rrhh_AvaluacionsCopy
    Inherits EviPage

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        'Valido que me llegue un idEvaluacion en el querystring
        If Request.QueryString("idEvaluacion") Is Nothing Then
            Response.Redirect("avaluacionsShow.aspx")
        ElseIf Request.QueryString("idEvaluacion") = String.Empty Then
            Response.Redirect("avaluacionsShow.aspx")
        End If

        '!! Validar que el cliente actual del profile tiene acceso a la evaluacion indicada en la url
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then CargarValoresEvaluacion()
    End Sub

    Private Sub CargarValoresEvaluacion()
        Dim sqlQuery As New SqlCommand("spEvaluacionesGetById")
        sqlQuery.CommandType = CommandType.StoredProcedure
        sqlQuery.Parameters.Add("@idEvaluacion", SqlDbType.UniqueIdentifier).Value = New Guid(Request.QueryString("idEvaluacion"))
        Dim rs As DataSet
        rs = RunQuery(sqlQuery)
        If rs.Tables(0).Rows.Count = 1 Then
            Dim row As DataRow
            row = rs.Tables(0).Rows(0)
            txtEvaluado.Text = row("UsuarioEvaluado")
            ddlEvaluadores.SelectedValue = row("GuidEvaluador").ToString
            ddlEvaluadores.Value = row("GuidEvaluador").ToString
            txtPeriodo.Text = row("AnyoEvaluacion")
            ddlPuestoTrabajo.Value = row("PuestoTrabajo")
            ddlPuestoTrabajo.Value = row("PuestoTrabajo")
            'txtFechaEntrega.SelectedDate = CDate(row("FechaEntrega"))
            txtFechaNacimiento.SelectedDate = CDate(row("FechaNacimiento"))
            ddlSexos.SelectedValue = row("Sexo").ToString
            ddlSexos.Value = row("Sexo").ToString
            txtEmail.Text = row("emailEvaluado").ToString
        End If
    End Sub

#Region "Procedimientos Sql"
    Function RunQuery(ByVal sqlQuery As SqlCommand) As DataSet
        Dim connectionString As String
        connectionString = _
            ConfigurationManager.ConnectionStrings _
            ("EVIMaster").ConnectionString
        Dim dbConnection As New SqlConnection
        dbConnection.ConnectionString = connectionString
        Dim dbAdapter As New SqlDataAdapter
        dbAdapter.SelectCommand = sqlQuery
        sqlQuery.Connection = dbConnection
        Dim resultsDataSet As DataSet = New DataSet
        Try
            dbAdapter.Fill(resultsDataSet)
        Catch ex As Exception
            '!!LabelStatus.Text = ex.Message.ToString
        End Try
        Return resultsDataSet
    End Function

    Function RunNonQuery(ByVal SqlCommandNonQuery As SqlCommand) As Integer
        Dim connectionString As String
        connectionString = _
            ConfigurationManager.ConnectionStrings _
            ("EVIMaster").ConnectionString
        Dim dbConnection As New SqlConnection
        dbConnection.ConnectionString = connectionString
        dbConnection.Open()
        SqlCommandNonQuery.Connection = dbConnection
        Dim rowsAffected As Integer = SqlCommandNonQuery.ExecuteNonQuery
        dbConnection.Close()
        Return rowsAffected
    End Function
#End Region

    Protected Sub btnGrabar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGrabar.Click
        Dim sqlQueryAdd As New SqlCommand("spEvaluacionesCopy")
        Dim newEvaluacion As System.Guid = Guid.NewGuid()
        sqlQueryAdd.CommandType = CommandType.StoredProcedure
        sqlQueryAdd.Parameters.Add("@idEvaluacion", SqlDbType.UniqueIdentifier).Value = New Guid(Request.QueryString("idEvaluacion"))
        sqlQueryAdd.Parameters.Add("@newidEvaluacion", SqlDbType.UniqueIdentifier).Value = newEvaluacion
        sqlQueryAdd.Parameters.Add("@idCliente", SqlDbType.UniqueIdentifier).Value = New Guid(Profile("Cliente").ToString)
        sqlQueryAdd.Parameters.Add("@UsuarioEvaluado", SqlDbType.NVarChar).Value = Me.txtEvaluado.Text
        sqlQueryAdd.Parameters.Add("@idUsuarioEvaluador", SqlDbType.NVarChar).Value = Me.ddlEvaluadores.Text
        sqlQueryAdd.Parameters.Add("@AnyoEvaluacion", SqlDbType.Int).Value = CInt(Me.txtPeriodo.Text)
        sqlQueryAdd.Parameters.Add("@PuestoTrabajo", SqlDbType.NVarChar).Value = Me.ddlPuestoTrabajo.Text
        sqlQueryAdd.Parameters.Add("@FechaEntrega", SqlDbType.DateTime).Value = CDate(System.DateTime.Now())
        sqlQueryAdd.Parameters.Add("@FechaEvaluacion", SqlDbType.DateTime).Value = CDate(System.DateTime.Now())
        sqlQueryAdd.Parameters.Add("@FechaNacimiento", SqlDbType.DateTime).Value = CDate(Me.txtFechaNacimiento.SelectedDate)
        sqlQueryAdd.Parameters.Add("@Sexo", SqlDbType.NChar).Value = Me.ddlSexos.Value
        sqlQueryAdd.Parameters.Add("@emailEvaluado", SqlDbType.NVarChar).Value = Me.txtEmail.Text


        Try
            Dim rowsCAdded As Integer = RunNonQuery(sqlQueryAdd)
            Response.Redirect("AvaluacionsEdit.aspx?idEvaluacion=" + newEvaluacion.ToString + "&Mode=Edit")
        Catch ex As Exception
            lblMsgError.Text = ex.Message.ToString
        End Try

    End Sub
End Class
